package com.intel.factorytest.activity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import java.io.File;
import java.io.FileOutputStream;
import android.os.SystemProperties;
import com.intel.factorytest.R;
import com.intel.factorytest.TestFileOp;
import com.intel.factorytest.TestShell;
import com.intel.factorytest.util.LogUtils;

public class LEDTest extends BaseActivity implements OnClickListener{

    private final String TAG = "LEDTest"; 
    
    private Button mRedButton;
    private Button mGreenButton;
    private Button mBlueButton;
    /*
    String RED_LED_DEV = "/sys/class/leds/red/brightness";
    String GREEN_LED_DEV = "/sys/class/leds/green/brightness";
    String BLUE_LED_DEV = "/sys/class/leds/blue/brightness";
    */
    String PROP_LED_RED_VALUE = "1";
    String PROP_LED_GREEN_VALUE = "2";
    String PROP_LED_BLUE_VALUE = "3";    
    String PROP_LED_OFF_VALUE = "0";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.led_test);
        initView();
        initEvent();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mRedButton.setEnabled(true);
        mGreenButton.setEnabled(true);
        mBlueButton.setEnabled(true);
    }

    private void initEvent() {
        
        mRedButton = (Button) findViewById(R.id.bt_red);
        mRedButton.setOnClickListener(this);
        mRedButton.setEnabled(false);
        mGreenButton = (Button) findViewById(R.id.bt_green);
        mGreenButton.setOnClickListener(this);
        mGreenButton.setEnabled(false);
        mBlueButton = (Button) findViewById(R.id.bt_blue);
        mBlueButton.setOnClickListener(this);
        mBlueButton.setEnabled(false);        
        setLED(PROP_LED_OFF_VALUE);
    }

    public void onClick(View v) {
        if (v.getId() == R.id.bt_red) {
            setLED(PROP_LED_OFF_VALUE);
            mRedButton.setEnabled(false);
            mGreenButton.setEnabled(true);
            mBlueButton.setEnabled(true);
            setLED(PROP_LED_RED_VALUE);
        } else if (v.getId() == R.id.bt_green) {
            setLED(PROP_LED_OFF_VALUE);
            mGreenButton.setEnabled(false);
            mRedButton.setEnabled(true);
            mBlueButton.setEnabled(true);
            setLED(PROP_LED_GREEN_VALUE);
        } else if (v.getId() == R.id.bt_blue) {
            setLED(PROP_LED_OFF_VALUE);
            mBlueButton.setEnabled(false);
            mRedButton.setEnabled(true);            
            mGreenButton.setEnabled(true);
            setLED(PROP_LED_BLUE_VALUE);
        }
    }

    /*
    final byte[] LIGHT_ON = new byte[255];
    final byte[] LIGHT_OFF = new byte[0];

    private void setLED(String led_device, boolean on) {        
        LogUtils.loge("setLED led_device = " + led_device + ", on = " + on);
        //led_device RED_LED_DEV  GREEN_LED_DEV  BLUE_LED_DEV
        try {
            FileOutputStream fos = new FileOutputStream(led_device);
            if(on) {
                fos.write(LIGHT_ON);
            }else {
                fos.write(LIGHT_OFF);
            }
            fos.close();   
        }catch (IOException e) {
            LogUtils.loge("setLED fail ");
        }
     
    }
    */
    private void setLED(String value) {
        // value 1 red 2 green 3 blue 0 off all
        int ledValueB = SystemProperties.getInt("persist.sys.ledctr",100);
        LogUtils.loge("setRedLED ledValueB = " + ledValueB);
        SystemProperties.set("persist.sys.ledctr", value);
        int ledValueE = SystemProperties.getInt("persist.sys.ledctr",200);
        LogUtils.loge("setRedLED ledValueE = " + ledValueE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        LogUtils.loge("onPause");
        setLED(PROP_LED_OFF_VALUE);
    }
    
    @Override
    protected void onDestroy() {
        LogUtils.loge("onDestroy");
        super.onDestroy();
    }

    @Override
    public void finish() {
        LogUtils.loge("finish is_pass = " + is_pass);
        tc.setTestResult(is_pass);
        super.finish();
    }

}

